package com.city.parking.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

public class ParkingProvider extends ContentProvider {
	public static final String AUTHORITY = "com.city.parking";
	
    private static final int PARKINGS = 1;
    private static final int PARKINGS_ID = 2;
    private static final int CARS = 3;
    private static final int CARS_ID = 4;

	private DBHelper mDBOpenHelper;
	private SQLiteDatabase db;
	
    private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        sURIMatcher.addURI(AUTHORITY, "parkings", PARKINGS);
        sURIMatcher.addURI(AUTHORITY, "parkings/#", PARKINGS_ID);
        sURIMatcher.addURI(AUTHORITY, "cars", CARS);
        sURIMatcher.addURI(AUTHORITY, "cars/#", CARS_ID);
    }
    
    private String getTableByUri(Uri uri) {
        int match = sURIMatcher.match(uri);
        String table = null;
        switch (match) {
        case PARKINGS:
        case PARKINGS_ID:
            table = DBHelper.TABLE_PARKINGS;
            break;
        case CARS:
        case CARS_ID:
            table = DBHelper.TABLE_CARS;
            break;
        default:
            return null;
        }
        
        return table;
    }

	@Override
	public boolean onCreate() {
		log("onCreate");
		mDBOpenHelper = new DBHelper(getContext());
		db = mDBOpenHelper.getReadableDatabase();
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sort) {
		log("query");
        Cursor ret = null;
		String table = getTableByUri(uri);
		if (table != null) {
		    ret = db.query(table, projection,
                    selection, selectionArgs, null, null, sort);
		}

		return ret;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		log("update");
        int count = 0;
        String table = getTableByUri(uri);
        if (table != null)
            count = db.update(table, values, selection, selectionArgs);
		return count;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		log("insert");
        String table = getTableByUri(uri);
        if (table != null) {
            long id = db.insert(table, null, values);
            return ContentUris.withAppendedId(uri, id);
        }
        return null;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		log("delete");
        String table = getTableByUri(uri);
		int count = db.delete(table, selection, selectionArgs);
		return count;
	}

	@Override
	public int bulkInsert(Uri uri, ContentValues[] values) {
		int numInserted = 0;

        String table = getTableByUri(uri);
		db.beginTransaction();
		try {
			for (ContentValues cv : values) {
				long newID = db.insertOrThrow(table, null, cv);
				if (newID <= 0) {
					throw new SQLException(
							"Failed to insert row into "
									+ uri);
				}
			}
			db.setTransactionSuccessful();
			getContext().getContentResolver().notifyChange(uri,
					null);
			numInserted = values.length;
		} finally {
			db.endTransaction();
		}
		return numInserted;
	}

	@Override
	public void shutdown() {
		mDBOpenHelper.close();
		db.close();
		super.shutdown();
	}

	@Override
	public String getType(Uri uri) {
		log("getType");
		return "channel_type";
	}

	private void log(String msg) {
		Log.i("Provider", msg);
	}
}
